﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using buildingLib.fee;
using Csla;
using buildingLib.security;

namespace _dmis.fee
{
    public partial class FeeTemplateDlg : _dmis.UIFormBase
    {
        #region ICommand
        public static string MenuItemName()
        {
            return "财务模板编辑";
        }
        public static Image MenuItemImage()
        {
            return _dmis.Properties.Resources.template.ToBitmap();
        }
        public static void MF_DoExcecute()
        {
            FeeTemplateDlg dlg = new FeeTemplateDlg();
            dlg.ShowDialog();
        }

        public static void MF_DoUpdateUI(ToolStripItem tsItem)
        {
            tsItem.Enabled = ACLTable.CanGet(FeeTemplate.GetACLID());
        }
        #endregion
        FeeTemplate m_currentTemplate;
        FeeTemplateList templateList;
        public FeeTemplateDlg()
        {
            InitializeComponent();
        }

        private void FeeTemplateDlg_Load(object sender, EventArgs e)
        {
            templateList = FeeTemplateList.GetLists("");
            feeTemplateListBindingSource.DataSource = templateList;
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            m_currentTemplate = FeeTemplate.NewFeeTemplate(true);
            feeTemplateBindingSource.DataSource = m_currentTemplate;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (m_currentTemplate == null)
            {
                MessageBox.Show("请选择要修改的模板！");
                return;
            }
            FeeTemplate.DeleteRoot(m_currentTemplate.Id);
            templateList = FeeTemplateList.GetLists("");
            feeTemplateListBindingSource.DataSource = templateList;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (m_currentTemplate == null)
            {
                MessageBox.Show("请选择要修改的模板！");
                return;
            }
            feeTemplateBindingSource.EndEdit();
            if (m_currentTemplate.IsNew)
                m_currentTemplate.Save();
            else
                m_currentTemplate.Update();

            templateList = FeeTemplateList.GetLists("");
            feeTemplateListBindingSource.DataSource = templateList;
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            if (m_currentTemplate == null)
            {
                MessageBox.Show("请选择要修改的模板！");
                return;
            }
            FeeTypeSelector dlg = new FeeTypeSelector(m_currentTemplate.Items);
            dlg.ShowDialog(this);
            dgvDetails[0, dgvDetails.Rows.Count - 1].Value = dlg.SelectedName;
        }

        private void btnDelItem_Click(object sender, EventArgs e)
        {
            if (m_currentTemplate == null)
            {
                MessageBox.Show("请选择要修改的模板！");
                return;
            }
            m_currentTemplate.Items.Remove((InvoiceItem)itemsBindingSource.Current);
        }

        private void dgvDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0)
                return;
            FeeTypeSelector dlg = new FeeTypeSelector();
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                ((InvoiceItem)itemsBindingSource.Current).TypeID = dlg.SelectedID;
                ((InvoiceItem)itemsBindingSource.Current).Name = dlg.SelectedName;
                dgvDetails[0, dgvDetails.Rows.Count - 1].Value = dlg.SelectedName;
            }
        }


        private void cbTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (feeTemplateListBindingSource.Current == null)
                return;
            m_currentTemplate = FeeTemplate.GetFeeTemplate(new Guid(cbTemplates.SelectedValue.ToString()));
            //DataSource = m_currentTemplate;
            feeTemplateBindingSource.DataSource = m_currentTemplate;
            //invoiceItemsBindingSource.DataSource = 
        }

    }
}